﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using CVsharp.Models.DAL.Account.Interface;
using CVsharp.Models.Entities;

namespace CVsharp.Models.DAL.Account
{
    public class SysActionPermission : IsysActionPermission
    {
        public  IList<sysActionPermission> GetAll()
        {
            using (DBEntities db = new DBEntities())
            {
                return db.sysActionPermission.ToList<sysActionPermission>();
            }
        }
        public IList<sysActionPermission> GetByUser(Guid userId)
        {
            using (DBEntities db = new DBEntities())
            {
                List<sysActionPermission> list = (from g in db.sysUserInActionPermission
                                   where g.UserId == userId
                                   select g.sysActionPermission).ToList();
#if DEBUG
                Console.WriteLine(db.sysUserInActionPermission.ToTraceString());
#endif
                return list;
            }
        }

        public IList<sysActionPermission> GetByRoid(string roleName)
        {
            using (DBEntities db = new DBEntities())
            {
                List<sysActionPermission> list = (from g in db.sysRoleInActionPermission
                                   where g.RoleName == roleName
                                   select g.sysActionPermission).ToList();
#if DEBUG
                Console.WriteLine(db.sysRoleInActionPermission.ToTraceString());
#endif
                return list;
            }
        }
        /// <summary>
        /// 根据多个角色名获取可访问列表
        /// </summary>
        /// <param name="roleNames"></param>
        /// <returns></returns>
        public IList<sysActionPermission> GetByRoids(string[] roleNames)
        {
            using (DBEntities db = new DBEntities())
            {
                List<sysActionPermission> list=new List<sysActionPermission>();
                foreach(string rolename in roleNames)
                {
                    list.AddRange((from g in db.sysRoleInActionPermission
                                   where g.RoleName == rolename
                                                      select g.sysActionPermission).ToList());
                    list = list.Distinct().ToList();
                }
#if DEBUG
                Console.WriteLine(db.sysRoleInActionPermission.ToTraceString());
#endif
                return list;
            }
        }
        /// <summary>
        /// 根据传入的权限列表同步修改数据库中的权限列表
        /// </summary>
        /// <param name="sysActionPermissionList">注册的Action数量</param>
        /// <returns></returns>
        public int Register(IList<sysActionPermission> sysActionPermissionList)
        {
            int result = 0;
            using (DBEntities db = new DBEntities())
            {
                foreach (var sap in sysActionPermissionList)
                {
                    sysActionPermission actionPermissionTemp =
    db.sysActionPermission.FirstOrDefault(
        m => m.ControllerName == sap.ControllerName&&m.ActionName==sap.ActionName);
                    if (actionPermissionTemp == null)
                    {
                        sap.ActionPermissionId = Guid.NewGuid();
                        if (sap.Description == null) 
                            sap.Description = string.Empty;
                        db.AddTosysActionPermission(sap);
                        result += db.SaveChanges();
                    }
                }
            }
            return result;
        }

        public bool Add(sysActionPermission actionPermission)
        {
            using (DBEntities db = new DBEntities())
            {
                if (actionPermission.Description == null)
                    actionPermission.Description = string.Empty;
                db.AddTosysActionPermission(actionPermission);
                int result = db.SaveChanges();
                return result == 1;
            }
        }

        public  bool DoObject(sysActionPermission actionPermission)
        {
            using (DBEntities db = new DBEntities())
            {
          
                sysActionPermission actionPermissionTemp =
                    db.sysActionPermission.FirstOrDefault(
                        m => m.ActionPermissionId == actionPermission.ActionPermissionId);

                if (actionPermissionTemp == null)//不存在则添加,存在则为修改
                {
                    db.AddTosysActionPermission(actionPermission);               
                }
                else
                {
                    actionPermissionTemp = actionPermission;
                }
                int result = db.SaveChanges();
#if DEBUG
#endif
                return result == 1;
            }
        }
        public bool Delect(sysActionPermission actionPermission)
        {
            using (DBEntities db = new DBEntities())
            {
                db.DeleteObject(actionPermission);
                int result = db.SaveChanges();
#if DEBUG
                Console.WriteLine(db.sysActionPermission.ToTraceString());
#endif
                return result == 1;
            }
        }
    }
}
